using System;

namespace Azure.Infrastructure
{
    public static class ConcurrencyExtensions
    {
        public static void Lock(this object lockObject, Action action)
        {
            lock (lockObject)
            {
                action();
            }
        }

        public static T RunIfNotSet<T>(this T item, object lockObject, Func<T> set) where T : class
        {
            T result = item;

            if (item == null)
            {
                lock (lockObject)
                {
                    if (item == null)
                    {
                        result = set();
                    }
                }
            }

            return result;
        }
    }
}